package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.AssessRecordBean;
import com.hxkcloud.patient.utils.ImageUtil;
import com.hxkcloud.patient.viewholders.AssessRecordViewholder;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by william_sun on 2016-07-07.
 * Description:评估记录列表适配器
 */
public class AssessRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<AssessRecordBean> datas;
    private LayoutInflater mInflater;

    private itemClickListener listener;
    private ResultSelectedListener resultListener;

    public AssessRecordAdapter(Context mContext, List<AssessRecordBean> datas,
                               itemClickListener listener, ResultSelectedListener resultListener) {
        this.mContext = mContext;
        this.datas = datas;
        this.listener = listener;
        this.resultListener = resultListener;
        mInflater = LayoutInflater.from(mContext);

    }

    public void setData(List<AssessRecordBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void cleanData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_assessrecordlist, null);
        AssessRecordViewholder holder = new AssessRecordViewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        AssessRecordViewholder viewholder = (AssessRecordViewholder) holder;
        AssessRecordBean bean = datas.get(position);

        viewholder.ll_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(position);
            }
        });
        viewholder.tv_name.setText(bean.getName());
        if (bean.getIsAnswer().equals("0")) {
            viewholder.tv_answer.setVisibility(View.VISIBLE);
        } else {
            viewholder.tv_answer.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(bean.getDate())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(new Date(Long.valueOf(bean.getDate())));
            viewholder.tv_date.setText(date);
        }
        if (bean.isDisplay()) {
            viewholder.btn_select.setVisibility(View.VISIBLE);
            viewholder.arrowIV.setVisibility(View.GONE);

            ((RelativeLayout.LayoutParams) viewholder.tv_date.getLayoutParams()).rightMargin = ImageUtil.dip2px(10, mContext);
        } else {
            viewholder.btn_select.setVisibility(View.GONE);
            viewholder.arrowIV.setVisibility(View.VISIBLE);

            ((RelativeLayout.LayoutParams) viewholder.tv_date.getLayoutParams()).rightMargin = ImageUtil.dip2px(30, mContext);
        }
        if (datas.get(position).isSelect()) {
//            viewholder.btn_select.setBackground(mContext.getResources().getDrawable(R.mipmap.btn_multiselect_pre));
            viewholder.btn_select.setBackgroundResource(R.mipmap.btn_multiselect_pre);
        } else {
//            viewholder.btn_select.setBackground(mContext.getResources().getDrawable(R.mipmap.btn_multiselect));
            viewholder.btn_select.setBackgroundResource(R.mipmap.btn_multiselect);
        }
        viewholder.btn_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (datas.get(position).isSelect()) {
                    resultListener.onSelected("no_select", position);
                } else {
                    resultListener.onSelected("select", position);
                }

            }
        });


        viewholder.gradeTV.setText(bean.getGrade());
        viewholder.scoreTV.setText(bean.getScore());
        viewholder.doctornameTV.setText(bean.getAnswerPerson());
        if ("2".equals(bean.getAnswerType())) {
            viewholder.doctornameTV.setVisibility(View.VISIBLE);
            viewholder.doctorTV.setVisibility(View.VISIBLE);
            viewholder.patientTV.setVisibility(View.GONE);
        } else {
            viewholder.doctornameTV.setVisibility(View.GONE);
            viewholder.doctorTV.setVisibility(View.GONE);
            viewholder.patientTV.setVisibility(View.VISIBLE);
        }

        if ("".equals(bean.getScore()) || bean.getScore() == null) {
            viewholder.scoreTV.setVisibility(View.GONE);
            viewholder.scoretextTV.setVisibility(View.GONE);
            if ("".equals(bean.getGrade()) || bean.getGrade() == null) {
                viewholder.gradeTV.setVisibility(View.GONE);
                viewholder.gradetextTV.setVisibility(View.GONE);
            } else {
                viewholder.gradeTV.setVisibility(View.VISIBLE);
                viewholder.gradetextTV.setVisibility(View.VISIBLE);
            }
        } else {
            viewholder.scoreTV.setVisibility(View.VISIBLE);
            viewholder.scoretextTV.setVisibility(View.VISIBLE);
            if ("".equals(bean.getGrade()) || bean.getGrade() == null) {
                viewholder.gradeTV.setVisibility(View.GONE);
                viewholder.gradetextTV.setVisibility(View.GONE);
            } else {
                viewholder.gradeTV.setVisibility(View.VISIBLE);
                viewholder.gradetextTV.setVisibility(View.VISIBLE);
            }
        }


        if (position == datas.size() - 1) {
            viewholder.bottomView.setVisibility(View.INVISIBLE);
        } else {
            viewholder.bottomView.setVisibility(View.VISIBLE);
        }

        if (position == 0) {
            viewholder.topView.setVisibility(View.VISIBLE);
        } else {
            viewholder.topView.setVisibility(View.GONE);
        }


    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public interface itemClickListener {
        void onItemClick(int position);
    }

    public interface ResultSelectedListener {
        void onSelected(String type, int position);
    }
}
